package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.OffLineShop.UI.OffLineShopDetailActivtiy;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.clearweed.ClearConsumeResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.view.personalCenter.ClearWeedDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/10.
 */

public class ClearWeedConsumeAdapter extends BaseAdapter {

    private Context context;
    private List<ClearConsumeResult.DataBean> list = new ArrayList<>();
    private Handler handler;
    private ClearConsumeResult.DataBean dataBean;

    public ClearWeedConsumeAdapter(Context context, List<ClearConsumeResult.DataBean> list, Handler handler) {
        this.context = context;
        this.list = list;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return list.size() > 0 ? list.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.clearweed_consume_layout, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (null != list.get(i) && !"".equals(list)) {
            dataBean = list.get(i);
            if (null != dataBean.getGoodsname() && !"".equals(dataBean.getGoodsname())) {
                holder.clearWeedConsumeName.setText(dataBean.getGoodsname());
            } else {
                holder.clearWeedConsumeName.setText("");
            }
            if (null != dataBean.getInnerprice() && !"".equals(dataBean.getInnerprice())) {
                holder.clearweedConsumePrice.setText(dataBean.getInnerprice() + "美豆");
            } else {
                holder.clearweedConsumePrice.setText("");
            }
            if(null!=dataBean.getRedbean()&&!"".equals(dataBean.getRedbean())){
                holder.clearWeedConsumeReadBean.setText(" + "+dataBean.getRedbean()+"红豆");
            }else{
                holder.clearWeedConsumeReadBean.setText("");
            }

            if (null != dataBean.getCtime() && !"".equals(dataBean.getCtime())) {
                holder.clearweedConsumeTime.setText("时间：" + dataBean.getCtime());
            } else {
                holder.clearweedConsumeTime.setText("");
            }
            if (null != dataBean.getShopname() && !"".equals(dataBean.getShopname())) {
                holder.clearWeedConsumeAddress.setText("店面：" + dataBean.getShopname());
            } else {
                holder.clearWeedConsumeAddress.setText("");
            }
            Glide.with(context).load(Url.IMG_URL + dataBean.getGoodsimage()).placeholder(R.mipmap.iv_eg).into(holder.clearweedConsumeImage);

            if (null != dataBean.getGoodsnum() && !"".equals(dataBean.getGoodsnum())) {
                holder.clearweedConsumeNum.setText("X" + dataBean.getGoodsnum());
            } else {
                holder.clearweedConsumeNum.setText("");
            }
            holder.clearWeedConsumeDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    AlertDialog dialog = new AlertDialog.Builder(context).setTitle("您确定要删除该项目吗？")
                            .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Message message = Message.obtain();
                                    message.obj = i;
                                    message.what = 0;
                                    handler.sendMessage(message);
                                    dialog.dismiss();
                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
                    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));
                    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.bg_yield));

                }
            });

        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ClearWeedDetailActivity.class);
                intent.putExtra("id", list.get(i).getGoodsid());
                context.startActivity(intent);
            }
        });
        return view;
    }


    static class ViewHolder {
        @Bind(R.id.clearweed_consume_image)
        ImageView clearweedConsumeImage;
        @Bind(R.id.clearWeed_consume_name)
        TextView clearWeedConsumeName;
        @Bind(R.id.clearweed_consume_price)
        TextView clearweedConsumePrice;
        @Bind(R.id.clearweed_consume_time)
        TextView clearweedConsumeTime;
        @Bind(R.id.clearWeed_consume_address)
        TextView clearWeedConsumeAddress;
        @Bind(R.id.clearweed_consume_num)
        TextView clearweedConsumeNum;
        @Bind(R.id.clearWeed_consume_delete)
        TextView clearWeedConsumeDelete;
        @Bind(R.id.clearweed_consume_readbean)
        TextView clearWeedConsumeReadBean;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
